/*
** ascii.c for ascii in /home/comet_a//afs/local/sys_unix/my_select/ex_1
** 
** Made by alexandre comet
** Login   <comet_a@epitech.net>
** 
** Started on  Thu Jan 12 16:02:49 2012 alexandre comet
** Last update Thu Jan 12 17:49:47 2012 alexandre comet
*/

#include	<termios.h>
#include	<unistd.h>

void		my_putchar(char c)
{
  write(1, &c, 1);
}

int		my_strlen(char *str)
{
  int		i;

  i = 0;
  while (str[i])
    i++;
  return (i);
}

void		my_putstr(char *str)
{
  write(1, str, my_strlen(str));
}

void		my_putnbr(int nb)
{
  if (nb < 0)
    {
      my_putchar('-');
      nb = nb * -1;
    }
  if (nb > 9)
    my_putnbr(nb / 10);
  my_putchar((nb % 10) + 48);
}

void		raw_mode(struct termios *t)
{
  t->c_lflag &= ~ICANON;
  t->c_cc[VMIN] = 1;
  if (tcsetattr(0, TCSANOW, t) == -1)
    write(2, "fail\n", 5);
}

int		main()
{
  char		buff[1024];
  int		r;
  int		x;
  int		i;
  struct termios	*t;

  tcgetattr(0, t);
  while (1)
    {
      raw_mode(t);
      my_putstr("Entrer du texte : ");
      r = read(0, buff, 1024);
      buff[r] = 0;
      while (buff[i])
	{
	  my_putstr("\nVoici le caractere : ");
	  my_putchar(buff[i]);
	  my_putstr("\n Et sa valeur ASCII : ");
	  x = buff[i];
	  my_putnbr(x);
	  my_putchar('\n');
	  i++;
	}
      i = 0;
    }
  return (0);
}
